package com.atguigu.flink.wordcount;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * Created by Smexy on 2022/11/18
 *
 *      有界流 == 批
 *          流式计算： 来一条数据，触发整个计算链路
 *          有界：     数据计算完，程序就退出。
 */
public class Demo2_BoundedStream
{
    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //设置并行度为1，全局只有一个Task计算
        env.setParallelism(1);
        //读取数据
        DataStreamSource<String> ds = env.readTextFile("data/words.txt");

        //转换
        ds.flatMap(new FlatMapFunction<String, Tuple2<String,Integer>>()
        {
            @Override
            public void flatMap(String value, Collector<Tuple2<String, Integer>> out) throws Exception {
                String[] words = value.split(" ");
                for (String word : words) {
                    out.collect(Tuple2.of(word,1));
                }
            }
        })
            //keyBy： 指定一个字段，按照这个字段作为key进行分组
          .keyBy(new KeySelector<Tuple2<String, Integer>, String>()
          {
              //返回的数据作为key，会被分组
              @Override
              public String getKey(Tuple2<String, Integer> value) throws Exception {
                  //f0代表tuple2的第一个字段
                  return value.f0;
              }
          })
          .sum(1)
          .print();


        //启动执行环境
        env.execute();

    }
}
